<template>
    <view class="content">

        <view class="cu-form-group between">
            <view class="title">名称</view>
            <input v-model="form.userName" placeholder="请输入"></input>
        </view>
       <!-- <view class="cu-form-group between">
            <view class="title">昵称</view>
            <input v-model="form.nickName" placeholder="请输入"></input>
        </view> -->

        <view class="cu-form-group between">
            <view class="title">手机号</view>
            <input v-model="form.phonenumber" placeholder="请输入手机号" type="number" maxlength="11"></input>
        </view>

        <view class="cu-form-group between">
            <view class="title">性别</view>
            <picker @change="bindPickerChange" :value="index" :range="array">
                <view class="uni-input">{{array[index]}}</view>
            </picker>

        </view>

        <view class="cu-form-group between">
            <view class="title">登陆密码</view>
            <input v-model="form.password" type="password" placeholder="请输入"></input>
        </view>
        <view class="cu-form-group between">
            <view class="title">确认密码</view>
            <input v-model="form.password2" type="password" placeholder="请输入"></input>
        </view>


        <view class="btn-box">
            <view class="submit-btn" @click="submit">保存</view>
        </view>
    </view>
</template>

<script>
    import api from '@/api/api.js';
    export default {
        data() {
            return {
                index: 0,
                array: ['男', '女'],
                form: {
                    "userName": "",
                    "nickName": "",
                    "phonenumber": "",
                    "sex": 0,
                    "password": "",
                    password2: '',
                    parentId: '',
                }
            }
        },
        onLoad(o) {
            if (o.createUserId) {
                this.form.parentId = o.createUserId
            }
        },
        methods: {
            bindPickerChange(e) {
                console.log(e);
                this.form.sex = this.index = e.detail.value
            },
            returnPickerType(e) {
                this.form.customerType = e.detail.value
            },
            returnPickerMethod(e) {
                this.form.handingMethod = e.detail.value
            },
            validatePhoneNumber(phoneNumber) {
                let reg = /^(13[0-9]|14[5-9]|15[0-1,5-9]|16[2,5-6]|17[1-8]|18[0-9]|19[1-9])\d{8}$/;

                if (reg.test(phoneNumber)) {
                    return true;
                } else {
                    return false;
                }
            },
            submit() {
                if (!this.form.userName) {
                    uni.showToast({
                        title: "请输入姓名",
                        icon: 'none',
                    });
                    return
                }
                // if (!this.form.nickName) {
                //     uni.showToast({
                //         title: "请输入昵称",
                //         icon: 'none',
                //     });
                //     return
                // }
                if (!this.validatePhoneNumber(this.form.phonenumber)) {
                    uni.showToast({
                        title: "请输入正确的手机号",
                        icon: 'none',
                    });
                    return
                }
                if (!this.form.password) {
                    uni.showToast({
                        title: "请输入密码",
                        icon: 'none',
                    });
                    return
                }
                if (!this.form.password2) {
                    uni.showToast({
                        title: "请再次输入密码",
                        icon: 'none',
                    });
                    return
                }

                if (this.form.password != this.form.password2) {
                    uni.showToast({
                        title: "两次输入的密码不一致",
                        icon: 'none',
                    });
                    return
                }
                let data = JSON.parse(JSON.stringify(this.form))

                api.insertUser(data).then(res => {
                    if (res.code == 200) {
                        this.toast('提交成功')
                    } else {
                        this.toast(res.msg)
                    }
                })


            }
        }
    }
</script>

<style scoped lang="scss">
    .content {
        margin-top: 20upx;

        .floow-cont {
            flex-direction: column;
            background-color: #ffffff;
            padding: 1rpx 30rpx;
            margin-top: 20upx;

            .title {
                text-align: justify;
                padding-right: 30upx;
                font-size: 30upx;
                position: relative;
                height: 60upx;
                line-height: 60upx;
            }

            textarea {
                width: 100%;
                height: 200upx;
                padding: 20upx 0;
            }
        }
    }

    .btn-box {
        padding: 20upx 24upx;

        .submit-btn {
            height: 80upx;
            line-height: 80upx;
            text-align: center;
            font-size: 28upx;
            color: #fff;
            border-radius: 8upx;
            background: #39b54a;
        }
    }
</style>